function IsMobileBrowse() {
    try {
        var userAgent = navigator.userAgent;
        if (userAgent == null || userAgent == '') return true;
        userAgent = userAgent.toLowerCase();
        if (/ipad/.test(userAgent)) return false;
        if (/mobile|symbian|android|phone|ucweb|mqqbrowser|nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|htc|dopod|juc|j2me|openwave|maui|untrusted|iuc|3gpp-gba|sunmicro|jblend|generic|oppo|mstar|techsoft|x11/.test(userAgent)) return true;
        return false;
    } catch(e) {
        return false;
    }
}
function goToWap() {
    if (hs_const_url.gotowap_url) {
        location = hs_const_url.gotowap_url;
    } else if (location.href.indexOf("http://www.hongshu.com/book/") != -1) {
        location = hs_const_domain.waphostName + "/book.php?bid=" + location.href.split('/')[4];
    } else if (location.href.indexOf("http://www.hongshu.com/bookreader/") != -1) {
        location = hs_const_domain.waphostName + "/chapterlist.php?bid=" + location.href.split('/')[4];
    } else if (location.href.indexOf("http://www.hongshu.com/content/") != -1) {
        if (location.href.split("/").length == 6) {
            location = hs_const_domain.waphostName + "/chapter_v2.php?bid=" + location.href.split("/")[4] + "&chpid=" + location.href.split("/")[5].split("-")[1].replace(/.html/g, "") + "&pn=0";
        } else if (location.href.split("/").length == 5) {
            location = hs_const_domain.waphostName + "/chapter_v2.php?bid=" + location.href.split("/")[4].replace(/.html/g, "");
        }
    } else if (location.href.indexOf("http://gg.hongshu.com") != -1) {
        location = hs_const_domain.waphostName + "/index.php?action=nan";
    } else if (location.href.indexOf("http://mm.hongshu.com") != -1) {
        location = hs_const_domain.waphostName + "/index.php?action=nv";
    } else if (location.href.indexOf("http://www.hongshu.com") != -1) {
        location = hs_const_domain.waphostName;
    } else {
        location = hs_const_domain.waphostName;
    }
}
var fadeinTime = 250;
var fadeoutTime = 250;
var fadetoggleTime = 250;
var setOutTime = 500; (function() {
    if (IsMobileBrowse() == true) {
        var refrefurl = document.referrer;
        var is_fromphone = CookieUtil.getCookie('fromphone');
        var other_need_goto = false;
        if (refrefurl != "") {
            if (/http:\/\/g.hongshu.com/.test(refrefurl) || /http:\/\/m.hongshu.com/.test(refrefurl)) {
                if (!is_fromphone) {
                    var date = new Date();
                    CookieUtil.setCookie({
                        'name': 'fromphone',
                        'value': 'true',
                        'expires': 86400 * 1000,
                        'path': '/',
                        'domain': '.hongshu.com'
                    });
                }
                is_fromphone = 'true';
            } else if (! (/hongshu.com/.test(refrefurl))) {
                other_need_goto = true;
            }
        }
        if (is_fromphone != 'true' || other_need_goto == true) {
            goToWap();
        }
    }
    
    setTimeout("initTopTip();", 500);
    setTimeout("bindHeadClick();", 500);
    setTimeout("initSearchTip();", 500);
})();
function showMenu() {
    if ($("#divUserMenu").css("display") == "none") {
        $("#divUserMenu").fadeIn(fadeinTime);
    } else {
        $("#divUserMenu").fadeOut(fadeoutTime);
    }
}
function bindHeadClick() {
    $("body").click(function() {
        if ($("#divUserMenu").css("display") == "block") {
            $("#divUserMenu").fadeOut(fadeoutTime);
        }
        if ($("#divSearchTip").css("display") == "block") {
            $("#divSearchTip").fadeOut(fadeoutTime);
        }
    });
}
function initTopTip() {
    if ($("#limediatip").html() != undefined) {
        $("#limediatip").hoverDelay({
            hoverEvent: function() {
                $("#divmediatip").fadeIn(fadeinTime);
            },
            outEvent: function() {
                $("#divmediatip").fadeOut(fadeoutTime);
            }
        });
    }
    if ($("#liwechattip").html() != undefined) {
        $("#liwechattip").hoverDelay({
            hoverEvent: function() {
                $("#divwechattip").fadeIn(fadeinTime);
            },
            outEvent: function() {
                $("#divwechattip").fadeOut(fadeoutTime);
            }
        });
    }
    $("#txtSkeyWords").keyup(function(event) {
        switch (event.keyCode) {
        case 13:
            goToSearch("", 0);
            break;
        }
    });
}
var booktiping = 0;
var authortiping = 0;
function initSearchTip() {
    if ($("#txtSkeyWords").val() != undefined) {
        $("#txtSkeyWords").bind("input propertychange",
        function() {
            var keyword = $.trim($(this).val());
            if (keyword.length >= 2) {
                var method = "search";
                var pclassid = "";
                var classid = "";
                var free = 0;
                var finish = 0;
                var charnum = 0;
                var updatetime = 0;
                var keywordtype = 2;
                var order = 4;
                var page = 1;
                var pagesize = 3;
                var submiturl = hs_const_url.homeajax;
                if (location.toString().toLowerCase().indexOf("https://") != -1) {
                    submiturl = hs_const_url.userajax;
                }
                if (booktiping == 0) {
                    $.ajax({
                        url: submiturl + '?callback=?',
                        data: {
                            method: method,
                            Pclassids: pclassid,
                            classids: classid,
                            free: free,
                            finish: finish,
                            charnum: charnum,
                            updatetime: updatetime,
                            keyword: keyword,
                            keywordtype: keywordtype,
                            order: order,
                            page: page,
                            pagesize: pagesize
                        },
                        type: 'post',
                        dataType: 'jsonp',
                        async: false,
                        beforeSend: function() {
                            booktiping = 1;
                            $("#divSearchTipBook").html("<h5 class=\"c2\">相关作品：</h5>");
                        },
                        success: function(data) {
                            if (data.bookinfo == null) {
                                $("#divSearchTipBook").append("<li><a href=\"javascript:void(0)\">暂无相关作品</a></li>");
                            } else {
                                for (var i = 0; i < data.bookinfo.length; i++) {
                                    var expression = eval('/' + keyword + '/');
                                    $("#divSearchTipBook").append("<li><a href=\"" + hs_const_domain.hostName + "/bookstore.html?keyword=" + encodeURIComponent(data.bookinfo[i].catename) + "&keytype=" + keywordtype + "\">" + data.bookinfo[i].catename.replace(expression, '<span class="c2">' + keyword + '</span>') + "</a></li>");
                                }
                            }
                        }
                    }).always(function() {
                        booktiping = 0;
                        if ($("#divSearchTip").css("display") == "none") {
                            $("#divSearchTip").fadeIn(fadeinTime);
                        }
                    });
                }
                keywordtype = 3;
                var pagesize = 15;
                if (authortiping == 0) {
                    $.ajax({
                        url: submiturl + '?callback=?',
                        data: {
                            method: method,
                            Pclassids: pclassid,
                            classids: classid,
                            free: free,
                            finish: finish,
                            charnum: charnum,
                            updatetime: updatetime,
                            keyword: keyword,
                            keywordtype: keywordtype,
                            order: order,
                            page: page,
                            pagesize: pagesize
                        },
                        type: 'post',
                        dataType: 'jsonp',
                        async: false,
                        beforeSend: function() {
                            authortiping = 1;
                            $("#divSearchTipAuthor").html("<h5 class=\"c2\">相关作者：</h5>");
                        },
                        success: function(data) {
                            if (data.bookinfo == null) {
                                $("#divSearchTipAuthor").append("<li><a href=\"javascript:void(0)\">暂无相关作者</a></li>");
                            } else {
                                var reali = 0;
                                for (var i = 0; i < data.bookinfo.length; i++) {
                                    if (reali >= 3) {
                                        break;
                                    }
                                    var expression = eval('/' + keyword + '/');
                                    if ($("a[data-name='" + escape(data.bookinfo[i].authorname) + "']").length == 0) {
                                        $("#divSearchTipAuthor").append("<li><a data-name=\"" + escape(data.bookinfo[i].authorname) + "\" href=\"" + hs_const_domain.hostName + "/bookstore.html?keyword=" + encodeURIComponent(data.bookinfo[i].authorname) + "&keytype=" + keywordtype + "\">" + data.bookinfo[i].authorname.replace(expression, '<span class="c2">' + keyword + '</span>') + "</a></li>");
                                        reali++;
                                    }
                                }
                            }
                        }
                    }).always(function() {
                        if ($("#divSearchTip").css("display") == "none") {
                            $("#divSearchTip").fadeIn(fadeinTime);
                        }
                        authortiping = 0;
                    });
                }
            } else {
                if ($("#divSearchTip").css("display") == "block") {
                    $("#divSearchTip").fadeOut(fadeinTime);
                }
            }
        });
    }
}
function goToSearch(keyword, keytype) {
    var _keyword = "";
    if (keyword == "") {
        _keyword = $.trim($("#txtSkeyWords").val());
    }
    if (_keyword == "") {
        location = hs_const_domain.hostName + "/bookstore.html";
    } else {
        var url = hs_const_domain.hostName + "/bookstore.html";
        var input = $('<input>').attr('type', 'text').attr('name', 'keyword').attr('value', _keyword);
        var form = $('<form>').attr('action', url).attr('method', 'post').append(input);
        if (keytype == 0) {
            form.submit();
        } else {
            var kt = $('<input>').attr('type', 'text').attr('name', 'keytype').attr('value', keytype);
            form.append(kt).submit();
        }
    }
}
var user_modellogin = {
    execaction: "",
    logining: 0,
    showLoginDialog: function(execaction) {
        $(".bodybg").fadeIn(fadeinTime,
        function() {
            $(".bodybg").click(function() {
                user_modellogin.closeLoginDialog();
            });
            $("#divModelLogin").css("position", "fixed").css("top", "120px").css("z-index", "1001").css("left", "50%").css("margin-left", "-235px");
            $("#divModelLogin").fadeIn(fadeinTime,
            function() {
                user_modellogin.execaction = execaction;
                $("#txtModelPassword").keyup(function(event) {
                    switch (event.keyCode) {
                    case 13:
                        user_modellogin.modelLogin();
                        break;
                    }
                });
                $("#txtModelUsername").focus();
            });
        });
    },
    closeLoginDialog: function() {
        $("#divModelLogin").fadeOut(fadeoutTime,
        function() {
            $("#divModelLogin").css("position", "").css("top", "").css("left", "");
            $("#txtModelUsername").val("");
            $("#txtModelPassword").val("");
            $("#liAlipayLogin").hide();
            $("#a_MoreloginTip").show();
            if (user_modellogin.execaction != 8 && user_modellogin.execaction != 9 && user_modellogin.execaction != 13 && user_modellogin.execaction != 14) {
                $(".bodybg").fadeOut(fadeoutTime);
            }
        });
    },
    forcecloseLoginDialog: function() {
        $("#divModelLogin").fadeOut(fadeoutTime,
        function() {
            $("#divModelLogin").css("position", "").css("top", "").css("left", "");
            $("#txtModelUsername").val("");
            $("#txtModelPassword").val("");
            $("#liAlipayLogin").hide();
            $("#a_MoreloginTip").show();
            $(".bodybg").fadeOut(fadeoutTime);
        });
    },
    showModelPassword: function() {
        if ($('#txtModelPassword').attr('type') == 'text') {
            $('#txtModelPassword').attr('type', 'password');
            $('#a_showModelEyes').removeClass('eye-open');
            $('#a_showModelEyes').addClass('eye-closed');
        } else {
            $('#txtModelPassword').attr('type', 'text');
            $('#a_showModelEyes').removeClass('eye-closed');
            $('#a_showModelEyes').addClass('eye-open');
        }
    },
    modelLogin: function() {
        if (this.logining == 0) {
            var username = $("#txtModelUsername").val();
            var password = $("#txtModelPassword").val();
            if (username.length == 0) {
                $("#divModelUsernameTip").html("用户名不可为空。");
                return;
            }
            $("#divModelUsernameTip").html("&nbsp;");
            if (password.length == 0) {
                $("#divModelPasswordTip").html("密码不可为空。");
                return;
            }
            $("#divModelPasswordTip").html("&nbsp;");
            var flag = $("#chkRemeber").prop("checked");
            var remeber = 0;
            if (flag == true) {
                remeber = 1;
            }
            var submiturl = hs_const_url.userajax;
            $.ajax({
                url: submiturl + "?method=" + encodeURIComponent("login") + "&username=" + encodeURIComponent(username) + "&password=" + encodeURIComponent(password) + "&remeber=" + encodeURIComponent(remeber) + "&callback=?",
                type: 'get',
                dataType: 'jsonp',
                async: false,
                beforeSend: function() {
                    user_modellogin.logining = 1;
                },
                success: function(data) {
                    if (data.code == 105) {
                        $("#divModelPasswordTip").html("登录成功");
                        UserManager.checkLogin(false,
                        function(user) {
                            var html = HtmlBuild.userInfo('index_user_info', user);
                            $("#loginInfoForm").html(html);
                            user_modellogin.closeLoginDialog();
                        });
                    } else if (data.code == -119) {
                        $("#divModelPasswordTip").html("密码错误，请重试。");
                    } else if (data.code == -116) {
                        $("#divModelUsernameTip").html("用户名不存在。");
                    }
                }
            }).always(function() {
                user_modellogin.logining = 0;
            });
        }
    }
}
function goToLogin(sign) {
    var url = hs_const_url.login + "?url=" + encodeURIComponent(location.href);
    if (!isNaN(sign)) {
        url = url + "&sign=" + sign;
    }
    location = url;
}
function goToLogout() {
    var url = hs_const_url.logout + "?url=" + encodeURIComponent(location.href);
    location = url;
}
function showLoading() {
    $(".bodybg").fadeIn(fadeinTime,
    function() {
        $("#floatingCirclesG").css("left", ((window.screen.width / 2) - 37.5)).css("top", ((window.screen.height / 2) - 150 + parseInt($(document).scrollTop())));
        $("#floatingCirclesG").show();
    });
}
function closeLoading() {
    if ($("#floatingCirclesG").css("display") == "block") {
        $(".bodybg").fadeOut(fadeoutTime,
        function() {
            $("#floatingCirclesG").hide();
        });
    }
}
if (top.location !== self.location) {
    top.location = self.location;
}